package root.electronclass.electronclass.personal.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.core.text.bold
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import root.electronclass.electronclass.R
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.BasicDialogFragment
import root.electronclass.electronclass.databinding.FragmentAchievementDetailDialogBinding
import root.electronclass.electronclass.databinding.ItemFractionBinding
import root.electronclass.electronclass.mvp.entity.ExamScoreRankVO
import root.electronclass.electronclass.mvp.entity.StuExamScoreDetail

/**
 *
 * @author Lokiy
 * @date 2023/2/18 21:10
 */
class AchievementDetailDialog : BasicDialogFragment<FragmentAchievementDetailDialogBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.CustomDialog)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val detail: StuExamScoreDetail = arguments?.getParcelable(DATA) ?: return
        binding.recycler.adapter = adapter
        adapter.refresh(detail.list)
        setTitle(detail.title, if (detail.type == 0) "日常测验" else "统一考试")
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(-1, -1)
    }

    companion object {
        const val DATA = "_DATA"
        const val TITLE = "_TITLE"
        const val CLOSE = "_CLOSE"
    }

}

@SuppressLint("SetTextI18n")
private val adapter = BasicAdapter<ExamScoreRankVO, ItemFractionBinding> { holder, item ->
    holder.binding.fraction.text = "${item.score}分"
    holder.binding.ranking1.text = buildSpannedString { scale(1.71f) { bold { item.classRank.toString() } }; append("\n班级排名") }
    holder.binding.ranking2.text = buildSpannedString { scale(1.71f) { bold { item.classRank.toString() } }; append("\n年级排名") }
    holder.binding.ranking3.text = buildSpannedString { scale(1.71f) { bold { item.classRank.toString() } }; append("\n全校排名") }
    holder.binding.subject.text = item.subject
    holder.binding.content.text = "较上次成绩${if (item.scoreUp < 0) "下降" else "上升"} ${item.scoreUp.toInt()}分"
    holder.binding.type.setBackgroundResource(if (item.scoreUp < 0) R.mipmap.decline else R.mipmap.icon_rise)
}